package com.baseproject;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.StrictMode;

import com.baseproject.component.AppComponent;
import com.baseproject.component.AppModule;
import com.baseproject.component.DaggerAppComponent;
import com.baseproject.datasource.DaggerRepositoryComponent;
import com.baseproject.datasource.RepositoryComponent;
import com.baseproject.datasource.RepositoryModule;
import com.baseproject.json.GsonProcessor;
import com.baseproject.json.JsonUtils;
import com.baseproject.util.AppUtils;
import com.baseproject.util.CrashHandler;
import com.facebook.stetho.Stetho;


public abstract class BaseApplication extends Application {

    protected static BaseApplication application;
    protected Activity currentShowingActivity;

    private AppComponent mAppComponent;
    private RepositoryComponent mRepositoryComponent;

    public static BaseApplication getApplication() {
        return application;
    }

    @Override
    public void onCreate() {
        enableStrickMode();
        super.onCreate();
        initApplication();
        CrashHandler.getInstance().init(getApplicationContext());
        Stetho.initializeWithDefaults(this);
        initCompoent();
        AppUtils.init(this);
        JsonUtils.getInstance().init(new GsonProcessor());
        mkDir();
        register();
    }

    private void enableStrickMode() {
        if (BuildConfig.STRICK_MODE_ENABLE) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectCustomSlowCalls() //API等级11，使用StrictMode.noteSlowCode
                    .detectDiskReads()
                    .detectDiskWrites()
                    .detectNetwork()   // or .detectAll() for all detectable problems
                    .penaltyLog() //在Logcat 中打印违规异常信息
                    .build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectLeakedSqlLiteObjects()
                    .detectLeakedClosableObjects() //API等级11
                    .detectActivityLeaks()
                    .penaltyLog()
                    .build());
        }
    }

    private void register() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                currentShowingActivity = activity;
            }

            @Override
            public void onActivityResumed(Activity activity) {
                currentShowingActivity = activity;
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    public Activity getCurrentShowingActivity() {
        return currentShowingActivity;
    }

    protected abstract void initApplication();

    /**
     * 如果不先创建app的根目录，会导致后面直接创建子目录失败，一次只能创建一级目录，否则失败
     */
    protected abstract void mkDir();

    protected void initCompoent() {
        mAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();

        mRepositoryComponent = DaggerRepositoryComponent.builder()
                .appModule(new AppModule(this))
                .repositoryModule(new RepositoryModule())
                .build();

    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }

    public RepositoryComponent getRepositoryComponent() {
        return mRepositoryComponent;
    }
}
